﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BiliBiliCSharpStudy.old {
    class OutParameterDemo {
        public static void demo() {
            //test1();
            test2();
        }

        public static void test2() {
            Student10 stu;
            bool b = StudentFactory1.Create("Jack", 60, out stu);
            if (b) {
                Console.WriteLine("Student {0}, age is {1}", stu.Name, stu.Age);
            }
        }

        public static void test1() {
            Console.WriteLine("please input first number: ");
            string str1 = Console.ReadLine();
            double x = 0;
            bool b1 = double.TryParse(str1, out x);
            if (!b1) {
                Console.WriteLine("input error!");
                return;
            }

            Console.WriteLine("please input second number: ");
            string str2 = Console.ReadLine();
            double y = 0;
            bool b2 = double.TryParse(str2, out y);
            if (!b2) {
                Console.WriteLine("input error!");
                return;
            }

            Console.WriteLine("{0} + {1} = {2}", x, y, (x + y));
        }
    }

    class DoubleParser {
        public static bool TryParse(string input, out double result) {
            try {
                result = double.Parse(input);
                return true;
            } catch {
                // 必须为 out 参数赋值，否则报错!
                result = 0;
                return false;
            }
        }
    }

    class Student10 {
        public int Age { get; set; }
        public string Name { get; set; }
    }

    class StudentFactory1 {
        public static bool Create(string stuName, int stuAge, out Student10 stu) {
            stu = null;
            if (string.IsNullOrEmpty(stuName)) {
                return false;
            }
            if (stuAge < 20 || stuAge > 80) {
                return false;
            }
            stu = new Student10() { Name = stuName, Age = stuAge };
            return true;
        }
    }
}
